<?php
/**
 * Setting parameters of templates in an article without leaving the article page.
 *
 * @author Stephan Gambke
 * @version 0.1 alpha
 */


if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This file is a MediaWiki extension, it is not a valid entry point.' );
}

if ( !defined( 'SF_VERSION' ) ) {
	die( 'This is a Semantic Forms extension. You need to install Semantic Forms first.' );
}

define( 'DE_VERSION', '0.1 alpha' );

// register extension
$wgExtensionCredits[ defined( 'SEMANTIC_EXTENSION_TYPE' ) ? 'semantic' : 'other' ][] = array(
	'path' => __FILE__,
	'name' => 'DirectEdit',
	'author' => array( '[http://www.mediawiki.org/wiki/User:F.trott Stephan Gambke]' ),
	'url' => 'http://www.mediawiki.org/wiki/Extension:DirectEdit',
	'descriptionmsg' => 'directedit-desc',
	'version' => DE_VERSION,
);


// extension settings
$defScriptPath = $wgScriptPath . "/extensions/DirectEdit";

// register extensions resources
$dir = dirname( __FILE__ );

$wgExtensionMessagesFiles['DirectEdit'] = $dir . '/DirectEdit.i18n.php';
$wgAutoloadClasses['DirectEdit'] = $dir . '/DirectEdit.body.php';
$wgAjaxExportList[] = 'DirectEdit::handleDirectEdit';
$wgAjaxExportList[] = 'DirectEdit::handleSubmit';

// register with ResourceLoader if defined
if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {

	$wgResourceModules['ext.DirectEdit.directedit'] = array(
		// JavaScript and CSS styles.
		'scripts' => 'libs/directedit.js',
		'styles' => 'skins/directedit.css',
		// When the module is loaded, these messages will be available to mediaWiki.msg()
		//'messages' => array( 'floatedit-desc' ),
		// Dependencies
		// listing jquery is not strictly necessary, but it does not hurt either
		'dependencies' => array( 'jquery' ),
		// subdir relative to "extensions" or $wgExtensionAssetsPath
		'localBasePath' => $dir,
		'remoteExtPath' => 'DirectEdit'
	);

		$wgResourceModules['ext.DirectEdit.submit'] = array(
		'scripts' => 'libs/submit.js',
		'styles' => 'skins/submit.css',
		'dependencies' => array( 'jquery' ),
		'localBasePath' => $dir,
		'remoteExtPath' => 'DirectEdit'
	);

};


# Define a setup function
$wgHooks['ParserFirstCallInit'][] = 'defParserFunction_Setup';

# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]       = 'defParserFunction_Magic';

//$wgHooks['BeforePageDisplay'][] = 'defCondLoadScripts';

function defParserFunction_Setup( &$parser ) {
	global $wgOut;

	// TODO: obsolete as of MW 1.16, remove around 1.18 or so
	wfLoadExtensionMessages( 'DirectEdit' );

	# Set a function hook associating the "directedit" magic word with our function
	$parser->setFunctionHook( 'directedit', 'DirectEdit::renderDirectEdit' );
	$parser->setFunctionHook( 'saveandcontinue', 'DirectEdit::renderSaveAndContinue' );

	// load jQuery
	$wgOut->includeJQuery();  // only necessary in MW 1.16, does nothing in MW 1.17+

	return true;
}

function defParserFunction_Magic( &$magicWords, $langCode ) {
        # Add the magic word
        # The first array element is whether to be case sensitive, in this case (0) it is not case sensitive, 1 would be sensitive
        # All remaining elements are synonyms for our parser function
        $magicWords['directedit'] = array( 1, 'directedit' );
        $magicWords['saveandcontinue'] = array( 1, 'saveandcontinue' );

        # unless we return true, other parser functions extensions won't get loaded.
        return true;
}

